package com.effectivsocial.service.impl;

import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.effectivsocial.domain.Lov;
import com.effectivsocial.repository.LovRepository;
import com.effectivsocial.service.api.AbstractService;
import com.effectivsocial.service.api.LovService;


@Named
public class LovServiceImpl extends AbstractService<Lov, String> implements LovService{

	@Inject
	private LovRepository lovRepository;
	
	public List<Lov> findByDeleted(Pageable pageable, boolean deleted) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void initInternal() {
		setRepository(lovRepository);
		
	}

	public Lov findByTypeAndName(String type, String name) {
		return lovRepository.findByTypeAndName(type, name);
	}
	
	@Transactional(propagation=Propagation.REQUIRED)
	public void checkAndLoad(List<Lov> entities, String type) {
		for(Lov lov : entities) {
			Lov lovTarget = lovRepository.findByTypeAndName(type, lov.getName());
			
			if(lovTarget == null) {
				lovRepository.save(lov);
			}
		}
		
	}

	
    public List<Lov> findByType(String type) {
            return lovRepository.findByType(type);
    }

	

}
